// pages/classifyList/classifyList.js
Page({
  data: {
    globalData: getApp().globalData,
    keyword: '',
    product: {},  //要加购的商品
    products: [],
    categoryId: '',
    keyword: '',
    pageNum: 1,
    pageSize: 10,
    total: 0,
    isLoading: false,
    carFlag: true,  //显示加购
    count: 1,  //加购数量
  },

  onLoad(options) {
    this.setData({
      categoryId: options.id == undefined ? '' : options.id, 
      keyword: options.keyword == undefined ? '' : options.keyword
    })
    this.getProducts(options.id)
  },
  onReachBottom() {
    if(this.data.pageNum * this.data.pageSize >= this.data.total) {
      return wx.showToast({
        title: '没有更多了~',
        icon: 'none'
      })
    }
    if(this.data.isLoading) return  // 判断是否正在加载其他数据
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getProducts(this.data.categoryId)
  },
  onPullDownRefresh() {
    this.setData({
      pageNum: 1,
      products: [],
      total: 0
    })
    this.getProducts(this.data.categoryId)
  },

  select() {
    this.setData({products: []})
    this.getProducts()
  },
  getProducts() {
    this.setData({isLoading: true})
    wx.showLoading({
      title: '数据加载中...',
    })
    var that = this
    wx.request({
      url: that.data.globalData.server + '/product/query',
      method: 'GET',
      data: {
        categoryId: this.data.categoryId,
        productName: this.data.keyword,
        status: 1,
        pageNum: this.data.pageNum, 
        pageSize: this.data.pageSize
      },
      success(res) {
        that.setData({
          products: [...that.data.products, ...res.data.data.records],
          total: res.data.data.total
        })
      },
      complete() {
        wx.hideLoading()
        that.setData({isLoading: false})
      }
    })
  },

  plus() {
    this.setData({count: this.data.count + 1})
  },
  minus() {
    if(this.data.count > 1) this.setData({count: this.data.count - 1})
  },
  showCar(e) {
    this.setData({carFlag: false, product: e.currentTarget.dataset.item})
  },
  hideCar() {
    this.setData({carFlag: true, count: 1})
  },
  joincar() {
    if(wx.getStorageSync('user') == '') {
      wx.showModal({
        title: '提示',
        content: '请登录后购买',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateTo({url: '/pages/login/login',})
          } else if (res.cancel) {
            return
          }
        }
      })
    } else {
      let that = this
      wx.request({
        url: that.data.globalData.server + '/cart',
        method: 'POST',
        data: {
          userId: wx.getStorageSync('user').userId,
          productId: this.data.product.productId,
          count: this.data.count
        },
        success(res) {
          that.hideCar()
          wx.showToast({
            title: '加购成功',
          })
        },
      })
    }   
  },
  toProduct(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({url: '/pages/productDetail/productDetail?id=' + id})
  },

})